﻿function main() {

	getBrowserInfo(); // 获取浏览器信息
	initObjects(); // 初始化页面控件对象
	initPage(); // 页面显示
	differBrowser(); // 针对不同浏览器，处理不同的样式
	bindFunctions(); // 绑定事件

	showPage(); // 隐藏“遮盖层”，显示页面
	objTxtUsername.focus();
//	
//	$(".chatFormButton").live("click", function(event){
//		sendChatContent(event);
//	});
//	
//	$(".enterChatContent").live("keypress", function(event){
//		sendChatContent(event);
//	});
//	
}

function getBrowserInfo() {

	screen_width = window.screen.width;
	screen_height = window.screen.availHeight;

	var browserString = navigator.userAgent.toLowerCase();

	var i;
	if ((i = browserString.indexOf("msie")) >= 0) {
		browser_type = "ie";
		browser_type += browserString.substring(i + 5, i + 6);
	}

	else if (browserString.indexOf("firefox") >= 0)
		browser_type = "firefox";
	else if (browserString.indexOf("chrome") >= 0)
		browser_type = "chrome";
	else if (browserString.indexOf("opera") >= 0)
		browser_type = "opera";
	else
		browser_type = "other";
}

function initObjects() {
	objTitleText = $("#loginTitle");
	objTxtHeart = $("#txtHeart");

	objLoginInputDIV = $("#loginInputDIV");
	objDivMyInfo = $("#divMyInfo");
	objFriendDIV = $("#friendDIV");
	objLoginDIV = $("#loginDIV");
	objBtnLogin = $("#btnLogin");
	objDivStateText = $("#divStateText");

	objTxtUsername = $("#txtUsername");
	objTxtPassword = $("#txtPassword");
	objChkKeepPassword = $("#chkKeepPassword");
	objCboBgImage = $("#cboBgImage");

	objBlotOutDIV = $("#blotOutDIV");
	objStatebar = $("#statebar");
	objImgMyFace = $("#imgMyFace");
	objDivMyNickname = $("#divMyNickname");
	objToolBar = $("#toolBar");

	objImgSearchUser = $("#imgSearchUser");
	objImgPersonSet = $("#imgPersonSet");
	objImgExitSystem = $("#imgExitSystem");
	objImgAboutSystem = $("#imgAboutSystem");
	objBody = $("body");
}

function initPage() {
	objTitleText.html(objTitleText.html() + VERSION);

	var i = public_getCookie("bgindex");
	if (isNaN(i)) {
		public_changeBG(1);
	} else {
		objCboBgImage.attr("selectedIndex", i - 1);
		public_changeBG(i);
	}

	objLoginDIV.css("margin-left",  (screen_width-240)/2);

}

function private_ChangeBG(i) {

	eval("$('body').css('background-image', 'url('+bgpath+i+')')");
}

function differBrowser() {

	// IE7不支持htc
	if (browser_type.indexOf("ie") >= 0) {
		if(browser_type != "ie7")
			objLoginDIV.css("behavior", "url(/style/PIE.htc)");
		if(browser_type == "ie6"){//IE6下png透明问题
			var s = document.createElement("script");
			$(s).attr("type", "text/javascript");
			$(s).attr("src", "/script/DD_belatedPNG_0.0.8a-min.js");
			$(s).appendTo($("head"));
			DD_belatedPNG.fix('.changeColor, .changeWeight, .selectFace');
		}	

	}

	if (browser_type == "ie7") {
		$(".windowtitleimage").css("filter", "gray");
		eval("DD_roundies.addRule('#loginDIV', '15px')");
		// eval("DD_roundies.addRule('#statebar', '10px')");
		objStatebar.css("filter", "alpha(opacity=70)"); // 状态栏透明度
	} else {
		$("body").css("overflow-x", "hidden"); // 网页不显示滚动条
		$("body").css("overflow-y", "hidden");
		
		objStatebar.css("opacity", "0.7"); // 状态栏透明度

	}

	if (browser_type == "ie7") {
	} else if (browser_type == "firefox") {
		objLoginDIV.css("-moz-border-radius", "15px");
		
		// objStatebar.css("-moz-border-radius", "10px");
	} else {
		objLoginDIV.css("border-radius", "15px");
		// objStatebar.css("border-radius", "10px");
	}


}

function bindFunctions() {
	
	objTxtHeart.focus(function(event) {
		focusTxtHeart();	
	});
	
	objTxtHeart.blur(function(event) {
		blurTxtHeart();	
	});
	
	
	objTxtHeart.keydown(function(event) {
		if (event.keyCode == 13)
			enterTxtHeart();
			
	});


	objBtnLogin.mouseover(function() {
		btnLoginOver();
	});

	objBtnLogin.mouseout(function() {
		btnLoginOut();
	});

	objBtnLogin.mousedown(function() {
		btnLoginDown();
	});

	objBtnLogin.mouseup(function() {
		btnLoginUp();
	});

	objBtnLogin.click(function() {
		validatorLogin();
	});

	objTxtUsername.mouseover(function() {
		txtInputOver($(this));
	});

	objTxtUsername.mouseout(function() {
		txtInputOut($(this));
	});

	objTxtPassword.mouseover(function() {
		txtInputOver($(this));
	});

	objTxtPassword.mouseout(function() {
		txtInputOut($(this));
	});

	objChkKeepPassword.focus(function() {
		$(this).blur();
	});

	objCboBgImage.change(function() {
		changeBG($(this).attr("value"));
	});

	objTxtUsername.keydown(function(event) {
		if(event.keyCode == 9)
			event.keyCode = 13;
		if (event.keyCode == 13)
			objTxtPassword.focus();	
			
	});
	
	$(".statebarImage").live("click", function(event) {
		event = event.target || event.srcElement;
		var id = event.id;
		if(id == "stateImgPersonSetWindow")
			showSetPersonWindow();
		else
			showChatWindow(public_getIdFromString($(this).attr("id")));
	});

	objTxtPassword.keydown(function(event) {
		if (event.keyCode == 13) {
			if (objTxtUsername.attr('value') == '')
				objTxtUsername.focus();
			else if (objTxtPassword.attr('value') == '')
				objTxtPassword.focus();
			else
				validatorLogin();
		}

	});
	
	$(".chatFormButton").live("click", function(event){
		sendChatContent(event);
	});
	
	$(".chatFormClearButton").live("click", function(event){
		clearContent(event);
	});
	
	
	$(".enterChatContent").live("keypress", function(event){
		if(event.ctrlKey && (event.which == 13 || event.which == 10))
			sendChatContent(event);
	});
	
	$(".enterChatContent").live("click", function(event){
		$(this).focus();
	});

	$(".minbox").live("click", function(event) {
		var src = event.target || event.srcElement;
		if(src.id == "personWindowMin")
			toMinPersonWindow();
		else
			toMin(public_getIdFromString(src.id));
	});

	$(".closebox").live("click", function(event) {
		var srcElement = event.target || event.srcElement;
		if(srcElement.id == "personWindowClose")
			closePersonWindow();
		else
			closeChatWIndow(public_getIdFromString(srcElement.id));

	});
	
	objImgSearchUser.click(function(event){
		alert("objImgSearchUser");
	});
	
	objImgPersonSet.click(function(event){
		var o = document.getElementById("personSetWindow");
		if(o != null)
			$(o).css("display", "block");
		else
			showSetPersonWindow();
	});
	
	objImgExitSystem.click(function(event){
		alert("objImgExitSystem");
	});
	objImgAboutSystem.click(function(event){
		alert("objImgAboutSystem");
	});

	//切换Tab效果
	$(".tabsBarButton").live("click", function(event){

		var srcElement = event.target || event.srcElement;
		var cid = srcElement.id;
		$(".personWindowTabsBar > button").removeClass("tabsBarButtonactive");
		$(srcElement).addClass("tabsBarButtonactive");

		$(".tabsContent").css("display", "none");
		$("#div" + cid.substring(3)).css("display", "block");

		$(srcElement).blur();
	});

	//单击图标，弹出聊天窗口
	$(".frienditem").live("click", function() {
		showChatWindow(public_getIdFromString($(this).attr("id")));
	});
}

function showPage() {
	objBlotOutDIV.remove();	//去除遮盖层
	objLoginDIV.css("display", "block");
}
